package com.tac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tac.common.Exception.CosTomException;
import com.tac.common.result.Result;
import com.tac.dao.StrategyDao;
import com.tac.pojo.*;
import com.tac.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.dc.pr.PRError;

import java.util.ArrayList;
import java.util.List;

@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyDao, Strategy> implements StrategyService {
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private FabulousService fabulousService;
    @Autowired
    private CollectionService collectionService;
    /**
     * 查看全部攻略
     * @return
     */
    public Result<IPage<Strategy>> selectAll(long page){
        LambdaQueryWrapper<Strategy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Strategy::getCreateTime);
        IPage<Strategy> pageInfo = new Page<>(page,10L);
        this.page(pageInfo,queryWrapper);
        List<Strategy> records = pageInfo.getRecords();
        ArrayList<Strategy> arrayList = new ArrayList<>();
        for(Strategy strategy:records){
            Long userId = strategy.getUserId();
            User user = userService.getById(userId);
            strategy.setUser(user);
            //设置收藏点赞和评论的数量
            LambdaQueryWrapper<Fabulous> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Fabulous::getTargetId,strategy.getId());
            lambdaQueryWrapper.eq(Fabulous::getType,"strategy");
            int count = fabulousService.count(lambdaQueryWrapper);
            strategy.setFabulous(count);

            LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Collection::getTargetId,strategy.getId());
            wrapper.eq(Collection::getType,"strategy");
            int count2 = collectionService.count(wrapper);
            strategy.setCollection(count2);

            LambdaQueryWrapper<Comment> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Comment::getTargetId,strategy.getId());
            wrapper2.eq(Comment::getType,"strategy");
            int count3 = commentService.count(wrapper2);
            strategy.setComment(count3);
        }
        return Result.success(pageInfo,"success");
    }


    /**
     * 写游记
     * @param strategy
     * @return
     */
    public Result saveStrategy(Strategy strategy,Long userId){
        User byId = userService.getById(userId);
        if(byId==null){
            throw  new CosTomException("用户不存在不能乱填没写token");
        }
        strategy.setUserId(userId);
        boolean save = this.save(strategy);
        if(!save){
            throw  new CosTomException("异常");
        }
        return Result.success(null,"success",201);
    }
    }
